home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / afloat.zip / ATOF.ASM < prev    next >
Assembly Source File  |  1988-03-14  |  4KB  |  210 lines

  1.         PAGE ,132
  2. ;----------------------------------------------------------
  3. ; ATOF -- version for use with assembly language programs
  4. ;
  5. ; Copyright Bob Kline 1988
  6. ;
  7. ; Purpose:
  8. ;    Convert ASCII string to floating point value
  9. ;
  10. ; Input:
  11. ;    SI points to start of string
  12. ;
  13. ; Output:
  14. ;    DX:AX contain 4-byte real
  15. ;
  16. ; Other registers used:
  17. ;    Values destroyed in BP, SI, DI, BX, CX
  18. ;
  19. ; Procedures:
  20. ;    FDECTOBIN
  21. ;----------------------------------------------------------
  22.     PUBLIC    ATOF
  23.     EXTRN    FDECTOBIN:NEAR
  24.  
  25.     .MODEL    SMALL
  26.  
  27. INDEX        EQU    SI
  28. CHARACTER    EQU    BYTE PTR [SI]
  29. DECIMALPLACES    EQU    DI
  30. ROUNDINGFLAG    EQU    BP
  31.  
  32.     .CODE
  33.  
  34. ATOF    PROC
  35.  
  36. ; initialize flags and counters
  37.     XOR    DECIMALPLACES,DECIMALPLACES
  38.     XOR    ROUNDINGFLAG,ROUNDINGFLAG
  39.  
  40. ; skip past leading spaces
  41. L2:     CMP     CHARACTER,' '
  42.         JNE     L1
  43.         INC     INDEX
  44.     JMP    L2
  45.  
  46. ; save sign of mantissa
  47. L1:    XOR    AX,AX
  48.         CMP     CHARACTER,'+'
  49.     JE    L0
  50.         CMP     CHARACTER,'-'
  51.     JNE    L3
  52.     INC    AX
  53. L0:     INC     INDEX
  54. L3:    PUSH    AX
  55.  
  56. ; zero out mantissa and check for digit in string
  57.     XOR    AX,AX
  58.     XOR    DX,DX
  59.         MOV     CX,10
  60.     JMP    SHORT L4
  61.  
  62. ; if mantissa not full use the digit
  63. L9:     TEST    DX,0F000h
  64.     JNZ    L5
  65.     PUSH    AX
  66.     XCHG    DX,AX
  67.     MUL    CX    ; 10
  68.     MOV    BX,AX
  69.     POP    AX
  70.         MUL     CX
  71.     ADD    DX,BX
  72.     MOV    BL,CHARACTER
  73.     AND    BX,0Fh
  74.     ADD    AX,BX
  75.     JNC    L6
  76.     INC    DX
  77.     JMP    SHORT L6
  78.  
  79. ; mantissa full: round up if necessary
  80. L5:    OR    ROUNDINGFLAG,ROUNDINGFLAG
  81.     JNZ    L7
  82.         INC     ROUNDINGFLAG
  83.         CMP     CHARACTER,'5'
  84.     JB    L7
  85.     INC    AX
  86.     JNC    L7
  87.     INC    DX
  88. L7:     DEC     DECIMALPLACES
  89.  
  90. ; check to see whether the next char is a digit and loop back if it is
  91. L6:     INC     INDEX
  92. L4:    CMP    CHARACTER,'0'
  93.     JB    L8
  94.         CMP     CHARACTER,'9'
  95.     JNA    L9
  96.  
  97. ; take care of digits after decimal point
  98. L8:     CMP     CHARACTER,'.'
  99.         JNE     L10
  100.         INC     INDEX
  101.     MOV    CX,10
  102.         JMP     SHORT L11
  103.  
  104. ; if mantissa not full use the digit
  105. L15:    TEST    DX,0F000h
  106.         JNZ     L12
  107.     PUSH    AX
  108.     XCHG    DX,AX
  109.     MUL    CX    ; 10
  110.     MOV    BX,AX
  111.     POP    AX
  112.     MUL    CX
  113.     ADD    DX,BX
  114.     MOV    BL,CHARACTER
  115.     AND    BX,0Fh
  116.     ADD    AX,BX
  117.         JNC     L13
  118.     INC    DX
  119. L13:    INC     DECIMALPLACES
  120.         JMP     SHORT L14
  121.  
  122. ; mantissa full -- round up if necessary
  123. L12:    OR    ROUNDINGFLAG,ROUNDINGFLAG
  124.         JNZ     L14
  125.         INC     ROUNDINGFLAG
  126.         CMP     CHARACTER,'5'
  127.         JB      L14
  128.     INC    AX
  129.         JNC     L14
  130.     INC    DX
  131.  
  132. ; check to see whether next character is a digit and loop back if it is
  133. L14:    INC     INDEX
  134. L11:    CMP     CHARACTER,'0'
  135.     JB    L10
  136.         CMP     CHARACTER,'9'
  137.     JNA    L15
  138.  
  139. ; put lower half of mantissa on stack so we can use AX to calculate
  140. ;   the exponent
  141. L10:    PUSH    AX
  142.     XOR    AX,AX
  143.  
  144. ; check for an exponent
  145.         CMP     CHARACTER,'e'
  146.     JE    L16
  147.         CMP     CHARACTER,'E'
  148.     JNE    L17
  149.  
  150. ; save the exponent's sign
  151. L16:    XOR    CX,CX
  152.         INC     INDEX
  153.         CMP     CHARACTER,'+'
  154.     JE    L18
  155.         CMP     CHARACTER,'-'
  156.     JNE    L19
  157.     INC    CX
  158. L18:    INC     INDEX
  159. L19:    PUSH    CX
  160.  
  161. ; calculate exponent
  162.     MOV    CL,10
  163.     JMP    SHORT L20
  164. L22:    MUL    CL
  165.         AND     CHARACTER,0Fh
  166.     ADD    AL,CHARACTER
  167.         INC     INDEX
  168.  
  169. ; see if character is another digit
  170. L20:    CMP     CHARACTER,'0'
  171.     JB    L21
  172.         CMP     CHARACTER,'9'
  173.     JNA    L22
  174.  
  175. ; get the sign for the exponent back from the stack, negating the
  176. ;   exponent if the sign is set
  177. L21:    POP    CX
  178.     OR    CX,CX
  179.     JZ    L17
  180.     NEG    AX
  181.  
  182. ; get the exponent into BX where it belongs, and adjust it for the
  183. ;   number of decimal places we found in the string
  184. L17:    MOV    BX,AX
  185.     SUB    BX,DECIMALPLACES
  186.  
  187. ; get the low word of the mantissa back into AX
  188.     POP    AX
  189.  
  190. ; get the sign back off the stack (remember, we stuck it there at
  191. ;   the very beginning of the procedure)
  192.     POP    CX
  193.  
  194. ; if the mantissa is zero, make sure the sign bit is zero
  195.     OR    AX,AX
  196.     JNZ    NOT_0
  197.     OR    DX,DX
  198.     JNZ    NOT_0
  199.     XOR    CX,CX
  200. NOT_0:
  201.  
  202. ; call fdectobin to convert the decimal components into
  203. ;   a 4-byte real
  204.     CALL    FDECTOBIN
  205.         RET
  206.  
  207. ATOF    ENDP
  208.  
  209.         END
  210.